package com.letu.aopcache.model;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 方法接口缓存封装
 *
 */
@Data
public class MethodCacheWrapper implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 最后加载时间
     * 当最后加载时间小于重启时间，则删除hash类型的缓存
     */
    private long lastLoadTime;

    /**
     * 缓存时长
     */
    private int expire;

    /**
     * 返回结果类型
     */
    private Class<?> returnType;
    /**
     * 返回结果类型
     */
    private String objectClass;

    /**
     * 结果id集合
     */
    private List<Long> ids;

    /**
     * 请求头信息，非业务参数
     */
    private Map<String,Object> header;

    /**
     * 类名
     */
    private String className;

    /**
     * 方法名
     */
    private String methodName;

    /**
     * 参数
     */
    private Object[] args;

    public MethodCacheWrapper() {
    }

    public MethodCacheWrapper(long lastLoadTime, int expire, Class<?> returnType, String objectClass, List<Long> ids, Map<String, Object> header, String className, String methodName, Object[] args) {
        this.lastLoadTime = lastLoadTime;
        this.expire = expire;
        this.returnType = returnType;
        this.objectClass = objectClass;
        this.ids = ids;
        this.header = header;
        this.className = className;
        this.methodName = methodName;
        this.args = args;
    }

    /**
     * 判断缓存是否已经过期
     *
     * @return boolean
     */
    public boolean isExpired() {
        if (expire > 0) {
            return (System.currentTimeMillis() - lastLoadTime) > expire * 1000;
        }
        return false;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        @SuppressWarnings("unchecked")
        MethodCacheWrapper tmp = (MethodCacheWrapper) super.clone();
        tmp.setReturnType(this.returnType);
        tmp.setHeader(this.header);
        tmp.setArgs(this.args);
        return tmp;
    }

}
